package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.tonbingchannel.config.*;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ChannelTonBingPb {

    public static ChannelTonBingProto.CTonBingSyncConfigRespMsg.Builder parseTonBingSyncConfigRespMsg(ChannelTonBingConfig config, String language) {
        ChannelTonBingProto.CTonBingSyncConfigRespMsg.Builder builder = ChannelTonBingProto.CTonBingSyncConfigRespMsg.newBuilder();
        for (ChannelTonBingBuffTypeConfig buffTypeConfig : config.getBuffTypeConfigMap().values()) {
            builder.addBuffTypeConfig(ChannelTonBingPb.parseChannelTonBingBuffTypeConfigMsg(buffTypeConfig, language));
        }

        for(int warType : config.getCityTypeConfigMap().keySet()){
            for (ChannelTonBingCityTypeConfig cityTypeConfig : config.getCityTypeConfigMap().get(warType).values()) {
                builder.addCityTypeConfig(ChannelTonBingPb.parseChannelTonBingCityTypeConfigMsg(cityTypeConfig, language));
            }
        }
        for(int warType : config.getCityConfigMap().keySet()){
            for (ChannelTonBingCityConfig cityConfig : config.getCityConfigMap().get(warType).values()) {
                builder.addCityConfig(ChannelTonBingPb.parseChannelTonBingCityConfigMsg(cityConfig, language));
            }
        }

        for (ChannelTonBingNpcEventConfig npcEventConfig : config.getNpcEventConfigMap().values()) {
            builder.addNpcEventConfig(ChannelTonBingPb.parseChannelTonBingNpcEventConfigMsg(npcEventConfig));
        }
        for (Map<Integer, ChannelTonBingTitleConfig> titleConfigMap : config.getTitleConfigMap().values()) {
            for(ChannelTonBingTitleConfig titleConfig : titleConfigMap.values()){
                builder.addTitleConfig(ChannelTonBingPb.parseChannelTonBingTitleConfigMsg(titleConfig, language));
            }
        }
        return builder;
    }

    public static ChannelTonBingProto.CTonBingBuffTypeConfigMsg.Builder parseChannelTonBingBuffTypeConfigMsg(ChannelTonBingBuffTypeConfig config, String language) {
        ChannelTonBingProto.CTonBingBuffTypeConfigMsg.Builder builder = ChannelTonBingProto.CTonBingBuffTypeConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setBuffType(config.getType());
        builder.setIcon(config.getIcon());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setDesc(ServerLanguageMgr.getContent(config.getDesc(), language));
        builder.setBuffEffect(config.getBuffEffect());
        builder.setContinueTime((int) (config.getContinueTime() / DateHelper.HOUR_MILLIONS));
        return builder;
    }

    public static ChannelTonBingProto.CTonBingCityTypeConfigMsg.Builder parseChannelTonBingCityTypeConfigMsg(ChannelTonBingCityTypeConfig config, String language) {
        ChannelTonBingProto.CTonBingCityTypeConfigMsg.Builder builder = ChannelTonBingProto.CTonBingCityTypeConfigMsg.newBuilder();
        builder.setCityType(config.getType());
        builder.setIcon(config.getIcon());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setFoodProduction(config.getFoodProduction());
        builder.setTeamNum(config.getTeamNum());
        builder.setSoldierNum(config.getSoldierNum());
        builder.setAddition(config.getAddition());
        builder.setBuffEffect(String.valueOf(config.getBuffEffect()));
        builder.setCampScore(config.getCampPoint());
        builder.setExtraTeamNum(config.getExtraTeamNum());
        builder.setWarType(config.getWarType());
        return builder;
    }

    public static ChannelTonBingProto.CTonBingCityConfigMsg.Builder parseChannelTonBingCityConfigMsg(ChannelTonBingCityConfig config, String language) {
        ChannelTonBingProto.CTonBingCityConfigMsg.Builder builder = ChannelTonBingProto.CTonBingCityConfigMsg.newBuilder();
        builder.setCityId(config.getId());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setMapType(config.getMapType());
        builder.setCityType(config.getType());
        builder.setInitialCamp(config.getInitialCamp());
        builder.setShowType(config.getShowType());
        builder.setSpecialAddition(config.getSpecialAddition());
        builder.setWarType(config.getWarType());
        return builder;
    }

    public static ChannelTonBingProto.CTonBingNpcEventConfigMsg.Builder parseChannelTonBingNpcEventConfigMsg(ChannelTonBingNpcEventConfig config) {
        ChannelTonBingProto.CTonBingNpcEventConfigMsg.Builder builder = ChannelTonBingProto.CTonBingNpcEventConfigMsg.newBuilder();
        builder.setEventId(config.getId());
        builder.setEventType(config.getType());
        builder.setIcon(config.getIcon());
        builder.setRewards(config.getRewards());
        return builder;
    }


    public static ChannelTonBingProto.CTonBingTitleConfigMsg.Builder parseChannelTonBingTitleConfigMsg(ChannelTonBingTitleConfig config, String language) {
        ChannelTonBingProto.CTonBingTitleConfigMsg.Builder builder = ChannelTonBingProto.CTonBingTitleConfigMsg.newBuilder();
        builder.setTitleId(config.getId());
        builder.setIcon(config.getIcon());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setType(config.getType());
        builder.setEffect(config.getEffect());
        builder.setRewards(config.getRewards());
        builder.setCampId(config.getCampId());
        return builder;
    }

    /**
     * 获取战力同步门客列表
     */
    public static List<ChannelTonBingProto.CTonBingPatronsTempMsg> parseTonBingPatronsListMsg(List<UserPatrons> userPatronsList) {
        List<ChannelTonBingProto.CTonBingPatronsTempMsg> list = new ArrayList<>();
        for (UserPatrons userPatrons : userPatronsList) {
            ChannelTonBingProto.CTonBingPatronsTempMsg.Builder tempBuilder = ChannelTonBingProto.CTonBingPatronsTempMsg.newBuilder();
            tempBuilder.setId(userPatrons.getPatronsId());
            tempBuilder.setSkin(userPatrons.getSkinId());
            tempBuilder.setAbility(userPatrons.getAbility().longValue());
            PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(userPatrons.getPatronsId());
            if(patronsInfo == null){
                continue;
            }
            tempBuilder.setOccId(patronsInfo.getOccupation());
            list.add(tempBuilder.build());
        }
        return list;
    }

}
